package common.intercepter.http;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import common.config.AppConfig;
import common.intercepter.entity.UimsInstitution;

import java.util.HashMap;
import java.util.Map;

public class RestTemplate {

    public static void main(String[] args) {

        String ip = "112.231.94.96";
        ip = "119.162.207.180";
        long time1 = System.currentTimeMillis();
        String msg = requestIpAuthority(ip);
        System.out.println("--耗时1:" + (System.currentTimeMillis() - time1));

        long time2 = System.currentTimeMillis();
        ip = "112.231.94.96";
        ip = "123.233.98.198";
//        ip = "119.162.207.180";
        msg = requestIpAuthority(ip);
        System.out.println("--耗时2:" + (System.currentTimeMillis() - time2));
        System.out.println(msg);
        JSONObject msgObj = JSONUtil.parseObj(msg);
        int code = msgObj.getInt("code");
        if (code == 0) {
            String data = msgObj.getStr("data");
            UimsInstitution uimsInst = JSONUtil.parseObj(data).toBean(UimsInstitution.class);
            System.out.println(uimsInst);
        }
    }

    public static String requestQFXCUserAllPermissions(Integer uid, Long time, String token) {
        //获取本系统的所有相关权限.
        String url = AppConfig.QFXCOPENAPIURLALL;
        Map<String, Object> resParamMap = new HashMap<>();
        resParamMap.put("token", token);
        resParamMap.put("time", time);
        resParamMap.put("uid", uid);
        String permissionStr = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Hutool http")
                .form(resParamMap)
                .timeout(2000000)
                .execute().body();
        return permissionStr;
    }

    public static String requestUserInfo(String token, long time) {
        String url = "https://user.tsgyun.com/api/getUserInfo";
//        Long timenow = System.currentTimeMillis();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("token", token);
        paramMap.put("time", time);
        //sitelist主键.
        paramMap.put("sid", AppConfig.SSO_ID);
        String result = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Hutool http")
                .form(paramMap)
                .timeout(20000)
                .execute().body();
        return result;
    }

    public static String requestUserPermissions(String token, Long time) {
        //获取本系统的所有相关权限.
        String url = "https://user.tsgyun.com/api/getResPermission";
//        Long timenow = System.currentTimeMillis();
        Map<String, Object> resParamMap = new HashMap<>();
        resParamMap.put("token", token);
        resParamMap.put("time", time);
        resParamMap.put("sid", AppConfig.SSO_ID);
        resParamMap.put("appid", AppConfig.APP_ID);

        String permissionStr = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Hutool http")
                .form(resParamMap)
                .timeout(20000)
                .execute().body();
        return permissionStr;
    }

    public static String requestIpAuthority(String ip) {
        //获取本系统的所有相关权限.
        String url = "https://user.tsgyun.com/api/verificationIp/" + ip;
        url = "https://user.tsgyun.com/api/verificationIp?ip=" + ip;
        String str = HttpRequest.get(url)
                .header(Header.USER_AGENT, "Hutool http")
                .timeout(20000)
                .execute().body();
        return str;
    }

}
